package com.five.community.controller;


import com.five.community.bean.Complaint;
import com.five.community.bean.Mailbox;
import com.five.community.bean.Village;
import com.five.community.bean.vo.ActivityVo;
import com.five.community.bean.vo.ComplaintVo;
import com.five.community.bean.vo.MailboxVo;
import com.five.community.contant.AppContants;
import com.five.community.service.ComplaintService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/complaint")
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;

    @Autowired
    private VillageService villageService;
    //投诉一览页面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map,Integer pageNum,String name) {
        if (pageNum == null) {
            pageNum = 1;
        }
            if (name == null || name == "") {
                try {
                    PageInfo<ComplaintVo> pageInfo = complaintService.getPageInfo(pageNum, AppContants.pageSize);
                    map.addAttribute("pageInfo", pageInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    PageHelper.startPage(pageNum, AppContants.pageSize);
                    List<ComplaintVo> complaintVos = complaintService.selectByName(name);
                    PageInfo<ComplaintVo> pageInfo = new PageInfo<>(complaintVos);
                    map.addAttribute("pageInfo", pageInfo);
                    Complaint complaint = new Complaint();
                    complaint.setName(name);
                    map.addAttribute("complaint", complaint);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        return "complaintlist";
    }
//    //投诉搜索根据名字
//    @RequestMapping("/selectUi")
//    public String selectUi(ModelMap map,String name,Integer pageNum){
//        if (name=="") {
//            return "redirect:/complaint/listUi";
//        } else {
//            try {
//                if (pageNum == null) {
//                    pageNum = 1;
//                }
//                PageHelper.startPage(pageNum,AppContants.pageSize);
//                List<ComplaintVo> complaintVos = complaintService.selectByName(name);
//                PageInfo<ComplaintVo> pageInfo = new PageInfo<>(complaintVos);
//                map.addAttribute("pageInfo", pageInfo);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        }
//        return "complaintlist";
//    }
    //投诉信息删除
    @RequestMapping("/limit/delete")
    public String delete(long id){
        try {
            complaintService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/complaint/listUi";
    }
    //添加页面
    @RequestMapping("/limit/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages", villages);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "complaint_add";
    }
    //添加投诉信息
    @RequestMapping("/add")
    public String add(ComplaintVo complaintVo){
        try {
            long vid = Long.parseLong(complaintVo.getVname());
            Complaint complaint = new Complaint(vid,complaintVo.getName(),complaintVo.getComplaintName(),complaintVo.getComplaintCause(),new Date(), complaintVo.isState());
            complaintService.insert(complaint);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/complaint/addUi";
    }
    //修改编辑页面
    @RequestMapping("/limit/editUi")
    public String editUi(long id, ModelMap map){
        try {
            Complaint complaint = complaintService.selectById(id);
            map.addAttribute("complaint",complaint);
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages",villages);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "complaint_edit";
    }
    //修改编辑投诉
    @RequestMapping("/edit")
    public String edit(ComplaintVo complaintVo){
        try {
            long vid = Long.parseLong(complaintVo.getVname());
            Complaint complaint = new Complaint(complaintVo.getId(),vid,complaintVo.getName(),complaintVo.getComplaintName(),complaintVo.getComplaintCause(),complaintVo.getCreateDate(), complaintVo.isState());
            complaintService.update(complaint);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "complaint_edit";
    }
}
